﻿using OneStationFile.IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;

namespace OneStationFile.WEB.Controllers
{
  
    public class RoleController : Controller
    {
        protected IBaseService BaseService { get; set; }
        // GET: Role

        #region AddNodes 添加节点
        /// <summary>
        /// AddNodes 添加节点
        /// </summary>
        /// <param name="strId"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult AddNodes(string strId, int id)
        {
            var result = new { msg = "添加成功！", res = true };
            if (string.IsNullOrEmpty(strId) || id <= 0)
            {
                result = new { msg = "请确认选中对的不为空,并再次提交！", res = false };
                return Json(result);
            }
            var arr = strId.Split(',');
            Model.Role role = BaseService.GetModelById<Model.Role>(id);
            List<Model.Node> nodes = new List<Model.Node>();
            foreach (string s in arr)
            {
                if (!string.IsNullOrEmpty(s))
                {
                    Model.Node nodeTemp = BaseService.GetModelById<Model.Node>(Convert.ToInt32(s));
                    if (nodeTemp != null)
                    {
                        //nodes.Add(nodeTemp);
                        role.Nodes.Add(nodeTemp);
                    }

                }
            }
            BaseService.SaveOrUpdate(role);
            return Json(result);
        }
        #endregion
        #region  DeleteNode 删除节点
        [HttpPost]
        /// <summary>
        /// DeleteNode 删除节点
        /// </summary>
        /// <param name="id"></param>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        public JsonResult DeleteNode(int id, int nodeId)
        {
            var result = new { msg = "删除成功！", res = true };
            Model.Role role = BaseService.GetModelById<Model.Role>(id);
            Model.Node node = BaseService.GetModelById<Model.Node>(nodeId);
            role.Nodes.Remove(node);
            BaseService.SaveOrUpdate(role);
            return Json(result);
        }
        #endregion
        /// <summary>
        /// FreeNodes 角色未拥有的节点
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="kwName"></param>
        /// <returns></returns>
        public ActionResult FreeNodes(int id = 0, int page = 1, string kwName = "")
        {
            Model.Role role = BaseService.GetModelById<Model.Role>(id);
            List<Model.Node> nodes = new List<Model.Node>();
            if (role.Nodes != null)
            {
                nodes = role.Nodes.ToList();
            }
            List<Model.Node> allNodes = BaseService.GetModels<Model.Node>().List().ToList();
            if (nodes.Count() >= 1)
            {
                allNodes = allNodes.Except(nodes).ToList();
            }

            PagedList<Model.Node> plm = allNodes.ToPagedList(page, 8);
            /// PagedList<Model.Modular> plm = bmu.GetListByPage(Where, "id desc ", page, 10).ToPagedList(page, 10);
            plm.CurrentPageIndex = page;
            plm.TotalItemCount = allNodes.Count();//总记录数
            return View(plm);
        }
        /// <summary>
        /// Nodes 角色所拥有的权限
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="kwName"></param>
        /// <returns></returns>
        public ActionResult Nodes(int id = 0, int page = 1, string kwName = "")
        {
            Model.Role role = BaseService.GetModelById<Model.Role>(id);
            List<Model.Node> nodes = new List<Model.Node>();
            if (role.Nodes != null)
            {
                nodes = role.Nodes.ToList();
            }
            PagedList<Model.Node> plm = nodes.ToPagedList(page, 8);
            /// PagedList<Model.Modular> plm = bmu.GetListByPage(Where, "id desc ", page, 10).ToPagedList(page, 10);
            plm.CurrentPageIndex = page;
            plm.TotalItemCount = nodes.Count();//总记录数
            return View(plm);
        }
        #region index，列表。
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="kwName"></param>
        /// <returns></returns>
        public ActionResult Index(int id = 0, int page = 1, string kwName = "")
        {

            List<Model.Role> queryOver = BaseService.GetModels<Model.Role>().List().ToList();
            if (!string.IsNullOrEmpty(kwName))
            {
                queryOver = queryOver.Where(p => p.Name.Contains(kwName)).ToList();
            }
            PagedList<Model.Role> plm = queryOver.ToPagedList(page, 8);
            /// PagedList<Model.Role> plm = bmu.GetListByPage(Where, "id desc ", page, 10).ToPagedList(page, 10);
            plm.CurrentPageIndex = page;
            plm.TotalItemCount = queryOver.Count();//总记录数
            return View(plm);
        }
        #endregion

        #region edit 编辑页面
        public ActionResult Edit(int id = 0)
        {
            if (id > 0)
            {
                ViewBag.action = "修改";
                Model.Role Role = BaseService.GetModelById<Model.Role>(id);
                return View(Role);
            }
            ViewBag.action = "添加";
            return View();
        }
        #endregion

        #region delete 删除（ajax）
        /// <summary>
        /// 删除（ajax）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]

        public JsonResult Delete(int id)
        {
            var result = new { msg = "删除成功!", res = true };
            if (id > 0)
            {
                if (!BaseService.DeleteModelById<Model.Role>(id))
                {
                    result = new { msg = "删除失败!", res = false };
                    return Json(result);
                }

            }
            return Json(result);
        }
        #endregion

        #region edithandle 添加或修改(ajax)
        [HttpPost]
        public JsonResult EditHandle(Model.Role model, FormCollection collection)
        {
            var result = new { msg = "操作成功!", res = true };
            Model.Role Role = null;
            if (string.IsNullOrEmpty(model.Name))
            {
                result = new { msg = "名称不能为空", res = false };
                return Json(result);
            }
           

            if (model.Id > 0)
            {
                if (BaseService.ExistModelByWhere<Model.Role>(p => p.Name == model.Name) >= 2)
                {
                    result = new { msg = "名称已经存在！", res = false };
                    return Json(result);
                }
                Role = BaseService.GetModelById<Model.Role>(model.Id);
                try
                {
                    var role = collection.GetValue("Role.Id");

                    UpdateModel(Role);
                }
                catch (Exception ex)
                {

                    throw (ex);
                }
            }
            else
            {
                if (BaseService.ExistModelByWhere<Model.Role>(p => p.Name == model.Name) >= 1)
                {
                    result = new { msg = "名称已经存在！", res = false };
                    return Json(result);
                }
                Role = model;
                //Role.Nodes = null;
            }
            if (BaseService.SaveOrUpdate(Role) == false)
            {
                result = new { msg = "保存时，出现在错误", res = false };
                return Json(result);
            }
            return Json(result);
        }
        #endregion
    }
}